<?php

declare(strict_types=1);

namespace app\admin\controller;

use think\facade\View; //模板引擎
use think\exception\ValidateException;
use app\common\Base;
use think\facade\Request;
use app\validate\AdminRoleVal;
use app\model\AdminRole as AdminRoleMod;

/**
 * @name 角色管理
 */
class AdminRole extends Base
{
    /**
     * @name 角色列表
     */
    public  function list()
    {
        if (request()->isPost()) {
            $param = Request::param();
            $w = '';
            if (isset($param['name']) && $param['name'] != '') {
                $w = "name like '%" . $param['name'] . "%'";
            }
            $list = AdminRoleMod::list($w, (int)$param['limit'], (int)$param['page']);
            return getBack(0, '', $list['data'], $list['count']);
        }
        return View::fetch();
    }

    /**
     * @name 角色添加
     */
    public function add()
    {
        if (request()->isPost()) {
            $param = Request::param();
            try { //进入验证
                validate(AdminRoleVal::class)->check($param);
            } catch (ValidateException $e) {
                return getBack(1, $e->getError());
            }
            $res = AdminRoleMod::add($param);
            if ($res) {
                return getBack(0);
            } else {
                return getBack(2);
            }
        }
        return View::fetch();
    }

    /**
     * @name 角色编辑
     */
    public function edit()
    {
        if (request()->isGet()) {
            $id = Request::param('id');
            $fild = AdminRoleMod::find($id);
            if ($fild) {
                View::assign('find', $fild);
            }
        }
        if (request()->isPost()) {
            $param = Request::param();
            try { //进入验证
                validate(AdminRoleVal::class)->check($param);
            } catch (ValidateException $e) {
                return getBack(1, $e->getError());
            }
            $res = AdminRoleMod::edit($param);
            if ($res) {
                return getBack(0);
            } else {
                return getBack(2);
            }
        }
        return View::fetch();
    }

    /**
     * @name 角色删除
     */
    public function del()
    {
        if (request()->isGet()) {
            $id = Request::param('id');
            $res = AdminRoleMod::del($id);
            if ($res=="success") {
                return getBack(0);
            } else {
                return getBack(3,$res);
            }
        }
    }

    /**
     * @name 权限角色设置
     */
    public function ability()
    {
        if (request()->isGet()) {
            $id = Request::param('id');
            $find = AdminRoleMod::fild($id);
            if ($find['ability'] != '') {
                $ability = explode(',', $find['ability']);
                // echo in_array('Dbd@dbdlist', $ability);
                View::assign('ability', $ability);
            } else {
                View::assign('ability', []);
            }
        }
        $path = base_path() . 'admin' . DIRECTORY_SEPARATOR . 'controller'; //控制器路径
        $pathList = glob($path . DIRECTORY_SEPARATOR . '*.php'); //模糊查询匹配
        // dump($pathList);
        $controllerList = [];
        foreach ($pathList as $k => $v) {
            if (basename($v, '.php') != 'Login') {
                $controllerList[] = ['title' => $this->get_class_annotation(basename($v, '.php'), 'admin'), 'controller' => basename($v, '.php')];
                // dump($this->get_class_annotation(basename($v, '.php'), 'admin'));
            }
        }
        foreach ($pathList as $k => $v) {
            if (basename($v, '.php') != 'Login') {
                $controller[] = basename($v, '.php');
            }
        }
        $base = get_class_methods('app\common\Base');
        $login = get_class_methods('app\admin\controller\Login');
        $filtration = array_merge($base, $login);
        foreach ($controller as $k => $v) {
            $arr = get_class_methods('app\admin\controller' . '\\' . $v);
            $actions[$v] = array_diff($arr, $filtration);
        }
        foreach ($actions as $k => $v) {
            foreach ($controllerList as $k1 => $v1) {
                if ($k == $v1['controller']) {
                    foreach ($v as $k2 => $v2) {
                        $arr1[$k][] = ['title' => $this->get_annotation('admin', $k, $v2), 'actions' => $v2];
                    }
                }
            }
        }
        // dump($controllerList);
        View::assign('controller', $controllerList);
        View::assign('actions', $arr1);
        if (request()->isPost()) {
            $param = Request::param('ability');
            $data['ability'] = implode(',', $param);
            $data['id'] = Request::param('id');
            $res = AdminRoleMod::edit($data);
            if ($res) {
                return getBack(0);
            } else {
                return getBack(2);
            }
        }
        return View::fetch();
    }
}
